/**
 * 
 */
package de.hfu.mdsd.mm.guimodel.reference;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 * @author vipse
 * 
 */
public class SwingReference {

	final static boolean shouldFill = true;
	final static boolean shouldWeightX = true;
	public static boolean RIGHT_TO_LEFT = false;

	public static void addComponentsToPane(Container pane) {

		if (!(pane.getLayout() instanceof BorderLayout)) {
			pane.add(new JLabel("Container doesn't use BorderLayout!"));
			return;
		}

		if (RIGHT_TO_LEFT) {
			pane.setComponentOrientation(java.awt.ComponentOrientation.RIGHT_TO_LEFT);
		}

		/*
		 * es werden 2 Tabs angelegt und alle Komponenten aufgeteilt:
		 * Tab1: Textfield, Textarea und ComboBox
		 * angelegt im GridbagLayout
		 * 
		 * Tab2: Checkbox und Liste
		 * angelegt im Gridbaglayout
		 */
		JTabbedPane tabbedPane = new JTabbedPane();
		JComponent panelFirstTab = new JPanel();
		tabbedPane.addTab("Tab 1", panelFirstTab);
		panelFirstTab.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		if (shouldFill) {
			// natural height, maximum width
			c.fill = GridBagConstraints.HORIZONTAL;
		}

		JLabel textFieldLabel = new JLabel("Textfield:");
		if (shouldWeightX) {
			c.weightx = 0.0;
			c.weighty = 0.0;
		}
		c.fill = GridBagConstraints.HORIZONTAL;
		c.insets = new Insets(0,10,10,0);
		c.gridx = 0;
		c.gridy = 0;
		panelFirstTab.add(textFieldLabel, c);
		
		JTextField textField = new JTextField(20);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 1.0;
		c.weighty = 1.0;
		c.insets = new Insets(0,0,10,10);
		c.gridx = 1;
		c.gridy = 0;
		panelFirstTab.add(textField, c);
		
		// Start Textarea
		JLabel textAreaLabel = new JLabel("TextArea:");
		if (shouldWeightX) {
			c.weightx = 0.0;
			c.weighty = 0.0;
		}
		c.fill = GridBagConstraints.HORIZONTAL;
		c.insets = new Insets(0,10,10,0);
		c.gridx = 0;
		c.gridy = 1;
		panelFirstTab.add(textAreaLabel, c);
		
		JTextArea textArea = new JTextArea(5, 20);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 0.5;
		c.weighty = 1.0;
		// up, left, down, right
		c.insets = new Insets(0,0,10,10);
		c.gridx = 1;
		c.gridy = 1;
		panelFirstTab.add(textArea, c);
		// End Textarea
		
		// Start Combobox
		JLabel ComboBoxLabel = new JLabel("Combobox:");
		if (shouldWeightX) {
			c.weightx = 0.0;
			c.weighty = 0.0;
		}
		c.fill = GridBagConstraints.HORIZONTAL;
		c.insets = new Insets(0,10,10,20);
		c.gridx = 0;
		c.gridy = 2;
		panelFirstTab.add(ComboBoxLabel, c);
		
		String[] petStrings = { "Texte", "Zahlen", "Bilder", "HTML"};
		JComboBox<String> petList = new JComboBox<String>(petStrings);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 0.5;
		c.weighty = 1.0;
		c.insets = new Insets(0,0,10,10);
		c.gridx = 1;
		c.gridy = 2;
		panelFirstTab.add(petList, c);
		// End Textarea
	
		JComponent panelSecondTab = new JPanel();
		tabbedPane.addTab("Tab 2", panelSecondTab);
		panelSecondTab.setLayout(new GridBagLayout());
		GridBagConstraints c2 = new GridBagConstraints();
		if (shouldFill) {
			// natural height, maximum width
			c2.fill = GridBagConstraints.HORIZONTAL;
		}

		JLabel checkBoxLabel = new JLabel("CheckBox:");
		if (shouldWeightX) {
			c2.weightx = 0.0;
			c2.weighty = 0.0;
		}
		c2.fill = GridBagConstraints.HORIZONTAL;
		c2.insets = new Insets(0,10,0,0);
		c2.gridx = 0;
		c2.gridy = 0;
		panelSecondTab.add(checkBoxLabel, c2);
		
		JPanel checkPanel = new JPanel();
		checkPanel.setLayout(new FlowLayout());
		JCheckBox checkBox = new JCheckBox("CheckBox");
		checkBox.setSelected(false);
		JCheckBox checkBox2 = new JCheckBox("CheckBox");
		checkBox2.setSelected(true);
		checkPanel.add(checkBox);
		checkPanel.add(checkBox2);
		c2.fill = GridBagConstraints.HORIZONTAL;
		c2.weightx = 0.5;
		c2.weighty = 1.0;
		c2.insets = new Insets(0,0,0,10);
		c2.gridx = 1;
		c2.gridy = 0;
		panelSecondTab.add(checkPanel, c2);
		
		JLabel listLabel = new JLabel("Liste:");
		if (shouldWeightX) {
			c2.weightx = 0.0;
			c2.weighty = 0.0;
		}
		c2.fill = GridBagConstraints.HORIZONTAL;
		c2.insets = new Insets(0,10,0,0);
		c2.gridx = 0;
		c2.gridy = 1;
		panelSecondTab.add(listLabel, c2);
		
		String[] eintraege = {"Texte", "Zahlen", "Bilder", "HTML"};
		JList<String> liste = new JList<String>(eintraege);
		c2.fill = GridBagConstraints.HORIZONTAL;
		c2.weightx = 0.5;
		c2.weighty = 1.0;
		c2.insets = new Insets(10,0,10,10);
		c2.gridx = 1;
		c2.gridy = 1;
		panelSecondTab.add(liste, c2);
		
		// Start tab3
		JPanel tab3 = new JPanel();
		tab3.setLayout(new FlowLayout());
		ArrayList<String> newtableColumnNames = new ArrayList<String>();
		newtableColumnNames.add("Bilder");
		newtableColumnNames.add("HTML");
		newtableColumnNames.add("Zahlen");
		ArrayList<String> newtableData = new ArrayList<String>();
		newtableData.add("Bilder");
		newtableData.add("HTML");
		newtableData.add("Zahlen");
		newtableData.add("Bilder");
		newtableData.add("HTML");
		newtableData.add("Zahlen");
		newtableData.add("Bilder");
		newtableData.add("HTML");
		newtableData.add("Zahlen");
		newtableData.add("Bilder");
		newtableData.add("HTML");
		newtableData.add("Zahlen");
		newtableData.add("Bilder");
		newtableData.add("HTML");
		newtableData.add("Zahlen");
		int jMaxnewtable = newtableData.size() / newtableColumnNames.size();
		Object[][] objnewtable = new Object[jMaxnewtable][newtableColumnNames
				.size()];
		int newtableCounter = 0;
		for (int i = 0; i < jMaxnewtable; i++) {
			for (int j = 0; j < newtableColumnNames.size(); j++) {
				objnewtable[i][j] = newtableData.get(newtableCounter);
				newtableCounter++;
			}
		}

		final JTable newtable = new JTable(objnewtable,
				newtableColumnNames.toArray());
		newtable.setPreferredScrollableViewportSize(new Dimension(500, 80));
		newtable.setFillsViewportHeight(true);
		
	    //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(newtable);
        //Add the scroll pane to this panel.
		tab3.add(scrollPane);
		tabbedPane.addTab("Tab3", tab3);
		

		// add tabbedPane to Center
		pane.add(tabbedPane, BorderLayout.CENTER);

		/* ******************************************************
		 * Ende Center 
		 ******************************************************* */
		 
		
		// South Area
		JButton okButton = new JButton("Speichern");
		JButton cancelButton = new JButton("Abbrechen");
		JPanel buttonPane = new JPanel();
        buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
        buttonPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 5, 10));
        buttonPane.add(Box.createHorizontalGlue());
        buttonPane.add(cancelButton);
        buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
        buttonPane.add(okButton);
		pane.add(buttonPane, BorderLayout.SOUTH);

	}

	/**
	 * Create the GUI and show it. For thread safety, this method should be
	 * invoked from the event dispatch thread.
	 */
	private static void createAndShowGUI() {

		// Create an set up the window
		JFrame frame = new JFrame("Prototyp Implementierung");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// set up the content pane
		addComponentsToPane(frame.getContentPane());
		// Display the window
		frame.pack();
		frame.setVisible(true);
	}

	public static void main(String[] args) {
		/* Use an appropriate Look and Feel */
		try {
			// UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
			UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
		} catch (UnsupportedLookAndFeelException ex) {
			ex.printStackTrace();
		} catch (IllegalAccessException ex) {
			ex.printStackTrace();
		} catch (InstantiationException ex) {
			ex.printStackTrace();
		} catch (ClassNotFoundException ex) {
			ex.printStackTrace();
		}
		/* Turn off metal's use bold fonts */
		UIManager.put("swing.boldMetal", Boolean.FALSE);

		// Schedule a job for the event dispatch thread:
		// creating and showing this application's GUI.
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				createAndShowGUI();
			}
		});

	}

}
